package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import com.example.fw.ApplicationManager;
import com.example.fw.ContactObject;
import com.example.fw.GroupObject;
import com.example.fw.Groups;

import org.testng.annotations.DataProvider;
import com.example.fw.GroupHelper;

public class ContactDataGenerator {
	
	public static String[] MONTHS = new String[]{"January", "February", "March",
			"April", "May", "June", "July", "August", "September", "October", "November", "December"};
	protected static int year = generateRandomNumberBetween(1930, 2013);
	protected static int day = generateRandomNumberBetween(01, 28);
	
	@DataProvider(name = "randomContactObject")
	public static Iterator<Object[]> generateRandomContactObject() throws InterruptedException {	
		Random rnd = new Random();				
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 2; i++) {			
			int group;
			ApplicationManager app = ApplicationManager.getInstance();
			GroupHelper groupHelperAssist = new GroupHelper(app);
			if (GroupHelper.groupModel != null){
				group = Math.abs((int)rnd.nextInt(GroupHelper.groupModel.size()+1));
			} else {
				Groups groupModel = groupHelperAssist.getGroups();
				group = Math.abs((int)rnd.nextInt(groupModel.size()));
			}
			String month = MONTHS[new Random().nextInt(12)];
			ContactObject contact = new ContactObject()
			.setFirstname("ivan" + (int)rnd.nextInt(100))
			.setLastname("Shchipalkin" + (int)rnd.nextInt(100))
			.setAddress("moscow355" + (int)rnd.nextInt(100))
			.setHome("asfgsdaga" + (int)rnd.nextInt(100))
			.setMobile("321545677" + (int)rnd.nextInt(100))
			.setWork("2155769" + (int)rnd.nextInt(100))
			.setEmail("123@mail.ru" + (int)rnd.nextInt(100))
			.setEmail2("fffff@gmail.com" + (int)rnd.nextInt(100))
			.setYear("" + year)
			.setAddress2("ghjfkdhkldfjlks" + (int)rnd.nextInt(100))
			.setPhone2("1-888-494-4499, Fax # 1-605-449-4419" + (int)rnd.nextInt(100))
			.setDay("" + day)
			.setMonth("" + month)
			.setGroupName(group);		
			list.add(new Object[]{contact});
		}
		return list.iterator();
	}
	
	@DataProvider(name = "ContactObjectFromFile")
	public static Iterator<Object[]> loadContactObjectFromFile() throws IOException {				
		List<Object[]> list = new ArrayList<Object[]>();
		BufferedReader reader = new BufferedReader(new FileReader("contacts.txt"));
		String line = reader.readLine();
		while (line != null){
			String[] parts = line.split("\t");
			ContactObject contact = new ContactObject()				
				.setFirstname(parts[0])
				.setLastname(parts[1])
				.setAddress(parts[2])
				.setHome(parts[3])
				.setMobile(parts[4])
				.setWork(parts[5])
				.setEmail(parts[6])
				.setEmail2(parts[7])
				.setYear(parts[8])
				.setAddress2(parts[9])
				.setPhone2(parts[10])
				.setDay(parts[11])
				.setMonth(parts[12]);
			list.add(new Object[]{contact});
			line = reader.readLine();
		}
		return list.iterator();
	}
	
	public static int generateRandomNumberBetween(int start, int end) {
		return new Random().nextInt(end - start) + start;
	}
	
	public static void main(String[] args){
		if (args.length < 2){
			System.out.println("Specify two parameters: file and count");
			return;
		}
		String file = args[0];
		int count = Integer.parseInt(args[1]);
		try {
			new ContactDataGenerator().generateDataToFile(file, count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void generateDataToFile(String fileName, int count) throws IOException {
		List<ContactObject> ContactObject =  generateRandomContactList(count);
		File file = new File(fileName);
		if (file.exists()){
			System.out.println("File exists, stop generator");
			return;
		} else {
			System.out.println("File has been created!");
		}
		FileWriter writer = new FileWriter(file);
		for (ContactObject contact : ContactObject) {
			writer.write(contact.firstname + "\t" + contact.lastname + "\t" 
					+ contact.address + "\t" + contact.home + "\t" + contact.mobile + "\t" 
					+ contact.work + "\t" + contact.email + "\t" + contact.email2 + "\t" 
					+ contact.year + "\t" + contact.address2 + "\t" + contact.phone2 + "\t" 
					+ contact.day + "\t" + contact.month + "\n");
			writer.flush();
		}
		writer.close();
		
	}

	private List<ContactObject> generateRandomContactList(int count) {
		List<ContactObject> list = new ArrayList<ContactObject>();
		for (int i = 0; i < count; i++) {
			Random rnd = new Random();	
			String month = MONTHS[new Random().nextInt(12)];
			ContactObject contact = new ContactObject()
			.setFirstname("ivan" + (int)rnd.nextInt(100))
			.setLastname("Shchipalkin" + (int)rnd.nextInt(100))
			.setAddress("moscow355" + (int)rnd.nextInt(100))
			.setHome("asfgsdaga" + (int)rnd.nextInt(100))
			.setMobile("321545677" + (int)rnd.nextInt(100))
			.setWork("2155769" + (int)rnd.nextInt(100))
			.setEmail("123@mail.ru" + (int)rnd.nextInt(100))
			.setEmail2("fffff@gmail.com" + (int)rnd.nextInt(100))
			.setYear("" + year)
			.setAddress2("ghjfkdhkldfjlks" + (int)rnd.nextInt(100))
			.setPhone2("1-888-494-4499, Fax # 1-605-449-4419" + (int)rnd.nextInt(100))
			.setDay("" + day)
			.setMonth("" + month);
			list.add(contact);
		}
		return list;
	}
}
